package com.hrg.rds.infra.connect.protocol.s7;

import com.hrg.rds.infra.connect.Properties4x;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;

// https://plc4x.apache.org/plc4x/latest/users/protocols/s7.html
// S7Configuration
@ConfigurationProperties(prefix = "s7")
@Data
public class S7Properties extends Properties4x {
	private boolean enabled = false;
	// S7-1200、1500 默认rack0+slot1，S7-300/400 默认rack0+slot2
    private String host = "127.0.0.1";
    private int port = 102;

	// private int rack = 0;
    // private int slot = 1;
	// private int readTimeout = 4;
	// private int pingTimeout = 2;
	// private int retryTimeout = 2;
	// private boolean tcpKeepAlive = true;
	// private boolean tcpNoDelay = true;
	// private int tcpDefaultTimeout = 4;

	private String extendedParams =
			"rack=0" +
			"&slot=1" +
			"&read-timeout=4_000" +
			"&ping=true" +
			"&ping-time=2_000" +
			"&retry-time=2_000" +
			"&tcp.keep-alive=true" +
			"&tcp.no-delay=true" +
			"&tcp.default-timeout=4_000";

	/**
	 * 连接字符串
	 * @return
	 */
	public String getConnId() {
		// protocolCode:transportCode://transportConfig?paramString
		// "^(?<protocolCode>[a-z0-9\\-]*)(:(?<transportCode>[a-z0-9]*))?://(?<transportConfig>[^?]*)(\\?(?<paramString>.*))?");
		return String.format("s7://%s:%d?%s",
				host, port, extendedParams);
	}

}
